package com.example.glideprogressiveloadimg

import okhttp3.Interceptor
import okhttp3.Response
import okhttp3.ResponseBody

class ProgressInterceptor : Interceptor {


    companion object {

        val LISTENER_MAP = hashMapOf<String, ProgressListener>()

        fun addListener(url:String, listener: ProgressListener) {
            LISTENER_MAP.put(url, listener)
        }

        fun removeListener(url:String) {
            LISTENER_MAP.remove(url)
        }
    }

    override fun intercept(chain: Interceptor.Chain): Response {

        val request = chain.request()
        val resposne :Response = chain.proceed(request)

        val url = request.url().toString()
        val body:ResponseBody? = resposne.body()
        val newResponse :Response =  resposne
            .newBuilder()
            .body(ProgressResponseBody(url, body))
            .build()
        return newResponse
    }

}